package vcp.gui.forms;

import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JLabel;

import vcp.gui.components.VCPPanel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import vcp.gui.forms.validators.NumericValidator;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.JButton;
import control.OrderController;
import control.SimpleClient;
import db.parking.beans.Orders;
import java.awt.Color;
import javax.swing.JRadioButton;



public class OrderSubscriptionForm extends VCPPanel implements ActionListener {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JTextField textCID;
	private JLabel lblNewOrderError;
	private SimpleClient client;
	private JRadioButton regularSRadioButton;
	private JRadioButton fullButton;
 	
	/* validators */
	private boolean isValidForm=false;
	private JTextField carNo_field;

	public OrderSubscriptionForm(SimpleClient client) {
		  buildForm();
		  this.client = client;
		  
	}
	
	private void buildForm(){

		textCID = new JTextField();
		textCID.setColumns(10);
		JLabel lblSID = new JLabel("Subscriber ID");
		JButton btnSend = new JButton("create");
		btnSend.addActionListener(this);
		btnSend.setActionCommand("btnSend");
		lblNewOrderError = new JLabel("");
		lblNewOrderError.setForeground(Color.RED);
		carNo_field = new JTextField();
		carNo_field.setColumns(10);
		JLabel lblCarNumber = new JLabel("Car Number");
		regularSRadioButton = new JRadioButton("Regular");
		fullButton = new JRadioButton("Full");
		
		GroupLayout groupLayout = new GroupLayout(this);
		groupLayout.setHorizontalGroup(
			groupLayout.createParallelGroup(Alignment.TRAILING)
				.addGroup(groupLayout.createSequentialGroup()
					.addContainerGap()
					.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
						.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
							.addGroup(groupLayout.createSequentialGroup()
								.addComponent(lblSID)
								.addContainerGap(365, Short.MAX_VALUE))
							.addGroup(groupLayout.createSequentialGroup()
								.addComponent(lblCarNumber, GroupLayout.PREFERRED_SIZE, 79, GroupLayout.PREFERRED_SIZE)
								.addContainerGap(365, Short.MAX_VALUE))
							.addGroup(groupLayout.createSequentialGroup()
								.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
									.addGroup(groupLayout.createSequentialGroup()
										.addGap(85)
										.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
											.addComponent(textCID, Alignment.TRAILING)
											.addComponent(carNo_field, Alignment.TRAILING, GroupLayout.DEFAULT_SIZE, 321, Short.MAX_VALUE)
											.addGroup(Alignment.TRAILING, groupLayout.createSequentialGroup()
												.addGap(44)
												.addComponent(regularSRadioButton)
												.addPreferredGap(ComponentPlacement.RELATED, 76, Short.MAX_VALUE)
												.addComponent(fullButton, GroupLayout.PREFERRED_SIZE, 79, GroupLayout.PREFERRED_SIZE)
												.addGap(65))))
									.addComponent(lblNewOrderError))
								.addGap(16)))
						.addGroup(Alignment.TRAILING, groupLayout.createSequentialGroup()
							.addComponent(btnSend)
							.addGap(181))))
		);
		groupLayout.setVerticalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addComponent(lblNewOrderError)
					.addGap(24)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblSID)
						.addComponent(textCID, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(18)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblCarNumber)
						.addComponent(carNo_field, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(34)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(fullButton)
						.addComponent(regularSRadioButton))
					.addGap(44)
					.addComponent(btnSend)
					.addContainerGap(72, Short.MAX_VALUE))
		);
		setLayout(groupLayout);

	}

	@Override
	public void actionPerformed(ActionEvent e) {
		
		Orders bean = new Orders();
		OrderController Orders;
		String type;
		
		//Add input Validators
		NumericValidator verifyID = new NumericValidator();
		
		    //Validate that the input is NOT empty
		   if((!regularSRadioButton.isSelected() && !fullButton.isSelected() ) || (regularSRadioButton.isSelected() && fullButton.isSelected())  )
		   {
			   JOptionPane.showMessageDialog(null,"Please Choose One Subscriber Type","Invalid Subscriber Error", JOptionPane.ERROR_MESSAGE);
			   isValidForm = false;
			   return;
		   }
			   
		   if(textCID.getText().isEmpty() || carNo_field.getText().isEmpty())
			{
				JOptionPane.showMessageDialog(null,"Please fill in all fields","Invalid Input Error", JOptionPane.ERROR_MESSAGE);
				isValidForm = false;
				return;
			}
			
		    //Parse all data	
			String ID = textCID.getText();
			String carNo = carNo_field.getText();
			

			//Input Validity Check
			if (ID.equals("") || carNo.equals("") || !verifyID.validate(ID) || !verifyID.validate(ID)) {
				JOptionPane.showMessageDialog(null,"Invalid ID Entered,Please Re-Enter","Invalid Input Error", JOptionPane.ERROR_MESSAGE);
				isValidForm = false;
			}
		   else{
				isValidForm = true;
			}
			if(regularSRadioButton.isSelected())
				type = "Regular";
			else
				type = "Full";

		 
			
			if(isValidForm==true){
				 try {
					Orders = new OrderController(client);
					bean.setUid(ID);
					bean.setType(type);
					bean.setCarNo(carNo);
					bean.setStatus(0); //TODO:what is the status of order when its been only set?
					//start the Order Controller to insert order into database
					Orders.InsertSubscriber(bean);
				} catch (Exception e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
		

		}
			
			
	}
}


